home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / Documents / NeXTAnswers / os.590 < prev    next >
Text File  |  1992-02-06  |  3KB  |  75 lines

  1. {\rtf0\ansi{\fonttbl\f0\fnil Times-Roman;\f3\fmodern Courier;\f1\fmodern Ohlfs;}
  2. \paperw11760
  3. \paperh7200
  4. \margl120
  5. \margr120
  6. {\colortbl\red0\green0\blue0;}
  7. \pard\tx1240\tx2480\tx3740\tx4980\tx6240\tx7480\tx8720\tx9980\tx11220\tx12480\f0\b0\i0\ul0\fs28\fc0 lock fcntl flock lockf\
  8. \
  9. Q: Why does 
  10. \f3 fcntl(fd, F_SETLK, &lockstruct)
  11. \f0  give 
  12. \f3 EINVAL
  13. \f0 ?\
  14.  
  15. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600 \
  16. Q:  What is the preferred method of file locking?\
  17. \
  18. A:  In the current release of  Sun's NFS file system has a number of problems regarding file locking.  Sun has implemented a "first crack" at solving the problems associated with extending Unix's file locking to NFS.  NeXT decided, upon looking at this, that there were too many hidden deadlock conditions inherent in the implementation to release.  Instead, the current version of 
  19. \f3\fs32 fcntl(2)
  20. \f0\fs28  will fail with 
  21. \f3 EINVAL
  22. \f0  if the directives 
  23. \f3 F_GETLK
  24. \f0  or 
  25. \f3 F_SETLK
  26. \f0  are used. These directives will work when a new version of NFS file locking is available,  in our 3.0 release.\
  27. \
  28. In the mean time, if a file locking system is necessary for your application, the best system to use is to write out a file whenever your application accesses a file for writing. The easiest thing to do is to create a file of the same name with an added extension, like "LCK" for instance.  Your application should check to see if this file exists before opening a file,  and create the lock file, if one doesn't exist. \
  29. \
  30. Here's one idea: if you open(2) a file with both CREATE and EXCLUSIVE turned on, then open(2) will return an error if the file exists, and will create the file if it doesn't.  Something like\
  31. \
  32.  
  33. \f3\fs24     #include <sys/file.h>\
  34.     int    fd;\
  35. \
  36.     fd = open(lockfile, O_CREAT | O_EXCL, 0600);\
  37.     \
  38.     if (fd < 0) \{                /* Something didn't work */\
  39.         if (errno != EEXIST) \{    /* Real error occurred */\
  40.             perror("Open failed");\
  41.             exit(1);\
  42.         \} \
  43.         else \{        /* Lock file is there     */\
  44.                 /* await lock release */\
  45.         \}\
  46.     \}\
  47.     else /* Things are OK */\
  48.  
  49. \f0\fs28 \
  50. This still has some problems when systems crash with files open, or the workspace is quit with your application still running.  To deal with this,  your application should erase these lock files (with the unlink(2) system call, for example) in an appDidTerminate: method. Also, when one of these lock files exist, you should put up an alert panel asking if the user would like to override the lock.\
  51. \
  52. For applications that run on a single machine (without any network file access) you can use  
  53. \f3\fs32 flock(2)
  54. \f0\fs28 if you are concerned that your application will be run multiple times on the same machine.  This scheme will work for network files too, when that support is added.\
  55. \
  56. \
  57.  
  58. \b Note:
  59. \b0  the 
  60. \pard\tx1240\tx2480\tx3740\tx4980\tx6240\tx7480\tx8720\tx9980\tx11220\tx12480\f3\fc0 fcntl
  61. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0 (2) directives not associated with file locking: \
  62.  
  63. \pard\tx1240\tx2480\tx3740\tx4980\tx6240\tx7480\tx8720\tx9980\tx11220\tx12480\f3\fc0     F_\{DUP,SET,GET\}FD\
  64.     F_\{GET,SET\}FL\
  65.     F_\{GET,SET\}OWN\
  66.  
  67. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0 work fine.\
  68. \
  69. QA590\
  70. \
  71. Valid for 1.0 \
  72. Valid for 2.0\
  73. \
  74.  
  75.